import { Modal } from "ant-design-vue";

/**
 * 树转数组
 * @param tree
 * @param childName
 * @returns
 * https://www.jianshu.com/p/5a7007436cf5
 */
export const treeTransToArray = (tree: any, childName = "children") => {
  if (
    !!!tree ||
    Object.prototype.toString.call(tree) !== "[object Array]" ||
    tree.length <= 0
  )
    return [];
  return tree.reduce(
    (pre: any, cur: any) =>
      pre.concat(cur, treeTransToArray(cur[childName], childName)),
    []
  );
};

/**
 * 对象属性值复制
 * @param source
 * @param target
 */
export const copyObjVal = (source: any, target: any) => {
  for (let key in source) {
    console.log("object key is " + key);
    if (target.hasOwnProperty(key)) {
      console.log("has key " + key);
      target[key] = source[key];
    }
  }
};

export const confirm = (
  title: string,
  content: string,
  ok: any,
  cancel: any
) => {
  Modal.confirm({
    title: title,
    content: content,
    onOk() {
      if (ok != null) {
        ok();
      }
    },
    onCancel() {
      if (cancel) {
        cancel();
      }
    },
  });
};

//获取当前时间戳
export const timestamp = (len: number = 10) => {
  if (len == 10) {
    return Date.parse(new Date().toString()) / 1000;
  } else {
    return new Date().valueOf();
  }
};
